angular.module('mamApp').controller('editContractManagementController', ['$scope', 'ContractManagementManager', '$state', '$uibModal','ImsDialog', "FileUploader","$filter","api",
    function ($scope, ContractManagementManager, $state, $uibModal,ImsDialog,FileUploader,$filter,api) {
    var  id=$state.params.id;
    $scope.formData={};
    ContractManagementManager.ContractEdit(id).then(function (result) {
        $scope.formData = result.data.adminContractVO;
        $scope.data=result.data.cpUserDetailVO;
        $scope.time=$scope.formData.singerDate,
        $scope.rangeDate = {
            startDate:moment($scope.formData.startDate).format('X'),
            endDate:moment($scope.formData.endDate).format('X'),
            onSelected: function (data, scope) {  //时间选择确认事件
                $scope.formData.startDate =data.startDate? moment(new Date(parseFloat(data.startDate) * 1000)).format("YYYY-MM-DD"):null;
                $scope.formData.endDate = data.endDate?moment(new Date(new Date(parseFloat(data.endDate) * 1000))).format("YYYY-MM-DD"):null;

            }
        };
    });
    $scope.openStart = function(item) {
        item.openedStart=true;
    };
    $scope.submit =function(){
        $scope.formData.singerDate =$filter('date')($scope.time,"yyyy-MM-dd");
        ContractManagementManager.ContractModify(id,$scope.formData).then(function (result) {
            if(result.data.c=="s"){
                $state.go('main.contractManagement',{},{reload:true})
            }
        })
    };

        $scope.contractLoading=false;
        angular.element(document.querySelector('#contractLoad')).on('change', function () {
            $scope.contractLoading = false;
        });

    $scope.contract = new FileUploader(
        {
            alias: "uploadFile",
            autoUpload:true,
            removeAfterUpload: true,//上传后删除文件 *!/
            filters: [{
                name: 'uploadFile',//后台接收字段
                fn: function (item, options) {
                    if (!/\.(pdf)$/.test(item.name)) {
                        $scope.contractLoading=false;
                        $scope.imageMsg = "最多只能上传1MB的pdf文件";
                        return false;
                    }else{
                        $scope.imageMsg = "";
                        $scope.contractLoading=true;
                        return true;
                    }
                }
            }],
            onSuccessItem: function (fileItem, response, headers) {
                if (response.c == "s") {
                    $scope.formData.fileUrl=response.fileVO.webUrl;
                    $scope.contractLoading=false;

                } else {
                    fileItem.isSuccess = false;
                    $scope.contractLoading=false;
                    alert("上传  " + fileItem.file.name + " 失败，请删除后重新添加上传");
                }
            },
            url:  api+"/admin/contract/image",
            headers: {'X-Auth-Token': $scope.userInfo.token}
        }
    );

    $scope.cancel=function () {
        $state.go('main.contractManagement',{},{reload:true})
    }
}])

